<template>
    <el-card shadow="never" class="border-0">


        <!-- 新增|刷新 -->
        <div class="flex items-center  mb-4">
            <el-button type="primary" size="small" @click="clickAddBtn(0)">新增</el-button>
        </div>

        <el-table :data="tableData" v-loading="tableDataLoading" :border="true" 
            :tree-props="{ children: 'children' }" row-key="id" :indent="30">

            <!-- <el-table-column prop="name" label="部门ID" #default="{ row }" align="center" >
                {{ row.id }}
            </el-table-column> -->

            <el-table-column prop="name" label="部门名称"  align="left" ></el-table-column>
 
            
            <el-table-column label="操作"  align="center" >
                <template #default="scope">
                    <div>
                        <el-button type="primary" size="small" text
                            @click="clickEditBtn(scope.row)">修改</el-button>

                        <el-button v-if="scope.row?.children != null" type="primary" size="small" text
                            @click="clickAddBtn(scope.row.id)">新增</el-button>

                        <el-popconfirm title="是否要删除该部门？" confirmButtonText="确认" cancelButtonText="取消"
                            @confirm="clickDeleteBtn(scope.row.id)">
                            <template #reference>
                                <el-button text type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>

        <!-- 新增 更新 抽屉 -->
        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="clickDrawerSubmitBtn">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="120px" :inline="false">
                <el-form-item required label="部门名称" prop="name">
                    <el-input v-model="form.name" placeholder="部门名称"></el-input>
                </el-form-item>
                <el-form-item label="上级部门ID" prop="description">
                    <el-input v-model="form.parentId" :disabled="true"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>
    </el-card>
</template>

<script setup>

import { ref, onMounted, reactive, computed } from 'vue'
import { api_department_tree, api_department_add, api_department_update, api_department_delete } from "~/api/departmentApi";
import FormDrawer from '~/components/FormDrawer.vue'
import { toast } from "~/composables/util"
import { useStore } from 'vuex'

// 页面变量
const store = useStore()
const tableData = ref([]) // 列表数据
const currentPage = ref(1)
const total = ref(0)
const limit = ref(3)
const tableDataLoading = ref(false)
// 校验
const rules = {
    name: [
      { required: true, message: '部门名称不能为空', trigger: 'change' }
    ]
}
const editId = ref(0)
const drawerTitle = computed(() => editId.value ? "修改" : "新增")
const searchForm = reactive({
    description: "",
    page: currentPage.value,
    size: limit.value,
})
const form = reactive({
    id: editId.value,
    parentId: 0,
    name: "",
})
const formRef = ref(null)
const formDrawerRef = ref(null)

// 生命周期
onMounted(() => {
    //初始化数据
    getData()
})

//获取列表数据
function getData(p = null) {
    if (typeof p == "number") {
        currentPage.value = p
    }
    tableDataLoading.value = true
    searchForm.page = currentPage.value
    api_department_tree()
    .then(res => {
        tableData.value = res
        
    })
    .finally(() => {
        tableDataLoading.value = false
    })
}


// 重置表单
function resetForm(row = false) {
    if (formRef.value) formRef.value.resetFields()
    if (row) {
        for (const key in form) {
            form[key] = row[key]
        }
    }
}

// 点击新增角色抽屉中 提交按钮
const clickDrawerSubmitBtn = () => {
    formRef.value.validate((valid) => {
        if (!valid) return
        console.log('form', form)
        formDrawerRef.value.showLoading()

        const fun = editId.value ? api_department_update(editId.value, form) : api_department_add({parentId: form.parentId, name: form.name})

        fun.then(res => {
            toast(drawerTitle.value + "成功")
            // 修改刷新当前页，新增刷新第一页
            getData(editId.value ? false : 1)
            formDrawerRef.value.close()
        }).catch(err => {

        }).finally(() => {
            formDrawerRef.value.hideLoading()
        })
    })
}

// 编辑
const clickEditBtn = (row) => {
    editId.value = row.id
    resetForm(row)

    formDrawerRef.value.open()
}

// 新增
const clickAddBtn = (parentId) => {
    //修改标记置为0
    editId.value = 0
    resetForm({
        parentId: parentId,
        name: "",
    })
    formDrawerRef.value.open()
}

// 删除
const clickDeleteBtn = (id) => {
    tableDataLoading.value = true
    api_department_delete(id).then(res => {
        toast("删除成功")
        getData()
    }).catch(err => {

    }).finally(() => {
        tableDataLoading.value = false
    })
}
</script>